//
//  HMSportGPSSingalButton.m
//  HMBest22
//
//  Created by Apple on 17/1/13.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "HMSportGPSSingalButton.h"
#import "HMSportTracking.h"
@implementation HMSportGPSSingalButton
- (void)awakeFromNib
{
    [super awakeFromNib];
    // 接收GPS信号改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeGPSStateNotification:) name:HMSportTrackingNotification object:nil];
}

// GPS信号改变
- (void)changeGPSStateNotification:(NSNotification *)notification
{
    // 获取GPS状态
    NSInteger state = [notification.object integerValue];
    
    NSString *imageName = _isMapButton ? @"ic_sport_gps_map_" : @"ic_sport_gps_";
    NSString *title;
    // 根据不同的枚举 设置不同的图片和文字
    switch (state) {
        case HMSportGPSSingalStateDisconnect:
            imageName = [imageName stringByAppendingString:@"disconnect"];
            title = @"  已断开连接";
            break;
        case HMSportGPSSingalStateBad:
            imageName = [imageName stringByAppendingString:@"connect_1"];
            title = @"  建议绕开高楼大厦";
            break;
        case HMSportGPSSingalStateNormal:
            imageName = [imageName stringByAppendingString:@"connect_2"];
            break;
        case HMSportGPSSingalStateGood:
            imageName = [imageName stringByAppendingString:@"connect_3"];
            break;
            
        default:
            break;
    }
    NSInteger right = (title == nil) ? 4 : 8;
    // 根据不同的文字设置不同的显示
    [self setContentEdgeInsets:UIEdgeInsetsMake(4, 4, 4, right)];
    // 给按钮赋值图片和文字
    [self setTitle:title forState:UIControlStateNormal];
    [self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
